Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / Boss Sime / BossSlimeRangedAttack.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BossSlimeRangedAttack : MonoBehaviour
{
    //GameObjects
    private GameObject target;
    public GameObject bulletPrefab;

    //Files
    private BossSlimeMovement bossslimeMovement;

    //Floats
    public float bulletOffset = 2.4f;
    public float bulletSpeed = 15;
    public float bulletCooldownTime = 0.8f;
    private float bulletShootTimer = 0;

    void Start()
    {
        //GameObjects
        target = GameObject.FindGameObjectWithTag("Player");

        //Files
        bossslimeMovement = FindObjectOfType<BossSlimeMovement>();
    }

    void Update()
    {
        if ((Vector3.Distance(transform.position, target.transform.position) <= bossslimeMovement.maxrange) && (bulletShootTimer <= 0))
        {
            Vector3 playerDirection = target.transform.position - transform.position;
            GameObject bullet = Instantiate(bulletPrefab, transform.position + (playerDirection.normalized * bulletOffset), Quaternion.identity);
            bullet.GetComponent<Rigidbody2D>().velocity = playerDirection.normalized * bulletSpeed;
            bulletShootTimer = bulletCooldownTime;
        }
        if (bulletShootTimer >= 0)
        {
            bulletShootTimer -= Time.deltaTime;
        }
    }
}